/**
 * 系统名称: 企业财资
 * 模块名称: repair-boot
 * 类 名 称: QuartzJobListener.java
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改人员:
 * 修改说明:
 * ============ ============================================
 * zhanghong 创建
 * ============ ============================================
 */
package com.mt.common.core.listener;

import com.mt.common.system.entity.ScheduledTaskLog;
import com.mt.common.system.entity.scheduledJob.JobConst;
import com.mt.common.system.service.ScheduledTaskLogService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

/**
 * @author zhanghong
 * @date 2023-03-22 20:34
 */
@Component
public class QuartzJobListener implements JobListener {

    @Autowired
    private ScheduledTaskLogService scheduledTaskLogService;


    private static ThreadLocal<ScheduledTaskLog> logThreadLocal = ThreadLocal.withInitial(ScheduledTaskLog::new);

    public QuartzJobListener(ScheduledTaskLogService scheduledTaskLogService) {
        this.scheduledTaskLogService = scheduledTaskLogService;
    }

    @Override
    public String getName() {
        return "quartzJobListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        ScheduledTaskLog scheduledTaskLog = new ScheduledTaskLog();
        scheduledTaskLog.setStartTime(new Date());
        logThreadLocal.set(scheduledTaskLog);
    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {

    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
        Trigger trigger = jobExecutionContext.getTrigger();
        JobDataMap jobDataMap = trigger.getJobDataMap();
        String beanName = (String) jobDataMap.get(JobConst.BEANNAME);
        String methodName = (String) jobDataMap.get(JobConst.METHODNAME);
        String methodParams = (String) jobDataMap.get(JobConst.METHODPARAMS);
        String jobName = (String) jobDataMap.get(JobConst.JOBNAME);
        String jobGroup = (String) jobDataMap.get(JobConst.JOBGROUP);
        String cron = (String) jobDataMap.get(JobConst.CRON);
        if (null != e) {
            ScheduledTaskLog scheduledTaskLog = logThreadLocal.get();
            scheduledTaskLog.setJobGroup(jobGroup);
            scheduledTaskLog.setJobName(jobName);
            scheduledTaskLog.setBeanName(beanName);
            scheduledTaskLog.setMethodName(methodName);
            scheduledTaskLog.setMethodParams(methodParams);
            scheduledTaskLog.setCron(cron);
            scheduledTaskLog.setResult("失败");
            StringWriter stringWriter=new StringWriter();
            PrintWriter printWriter=new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            scheduledTaskLog.setFailReason(stringWriter.toString());
            scheduledTaskLog.setEndTime(new Date());
            scheduledTaskLogService.saveScheduledTaskLog(scheduledTaskLog);
            logThreadLocal.remove();
            return;
        }
        ScheduledTaskLog scheduledTaskLog = logThreadLocal.get();
        scheduledTaskLog.setJobGroup(jobGroup);
        scheduledTaskLog.setJobName(jobName);
        scheduledTaskLog.setBeanName(beanName);
        scheduledTaskLog.setMethodName(methodName);
        scheduledTaskLog.setMethodParams(methodParams);
        scheduledTaskLog.setCron(cron);
        scheduledTaskLog.setResult("成功");
        scheduledTaskLog.setEndTime(new Date());
        scheduledTaskLogService.saveScheduledTaskLog(scheduledTaskLog);
        logThreadLocal.remove();
    }
}
